package cloudPayAdmin.admin.controller.hicatcard.user;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.card.TCard;
import cloudPayAdmin.admin.dbapp.entity.hicatcard.user.TUserCardRechargeNewUser;
import cloudPayAdmin.admin.service.hicatcard.card.CardService;
import cloudPayAdmin.util.AdminLteDataTableResp;
import cloudPayAdmin.util.AdminLteUtil;
import cloudPayAdmin.util.TimeUtil;
import cloudPayAdmin.util.WebUtil;
import cloudPayAdmin.util.pagebean.HqlPageBeanHelper;
import cloudPayAdmin.util.pagebean.HqlParam;
import cloudPayAdmin.util.pagebean.PageBean;

@Controller
@RequestMapping("/hicatcard/userCardRechargeNewUser")
public class UserCardRechargeNewUserController {
	
	@Autowired
	HqlPageBeanHelper hqlPageBeanHelper;

	@PersistenceContext(unitName = "entityManagerFactory")
	EntityManager entityManager;
	
	@Autowired
	Environment env;
	
	@Autowired
	CardService cardService;
	
	@RequestMapping(value = "/toUserCardRechargeNewUserListPage")
	public String toUserCardRechargeListPage(HttpServletRequest request, Map<String, Object> model) {		
		request.setAttribute("statusMap", TUserCardRechargeNewUser.initStatusMap());
		return "hicatcard/userCardRecharge/user-card-recharge-new-user-list";
	}
	
	@RequestMapping(value = "/getUserCardRechargeNewUserListData")
	@ResponseBody
	public String getUserCardRechargeListData(HttpServletRequest request,Long userCardId,String systemPresentCardNumber) {
		
		Map<String, Object> paramMap = WebUtil.getSearchParam(request);		
		paramMap = WebUtil.formatDateRange(paramMap, TimeUtil.DATE_PATTERN_NOSEPARTOR);
		// 需要映射的实体类
		HqlParam hqlParam = WebUtil.buildHqlParam(paramMap, TUserCardRechargeNewUser.class, request);
		PageBean<TUserCardRechargeNewUser> cloudpayCardPage = hqlPageBeanHelper.findHqlPageBean(entityManager, hqlParam);			
		
		for(TUserCardRechargeNewUser userCardRechargeNewUser : cloudpayCardPage.getContent()) {
			
			TCard card = cardService.findById(userCardRechargeNewUser.getCardId());
			if(card != null) {
				userCardRechargeNewUser.setCardName(card.getCardName());
			}
			
		}
		
		AdminLteDataTableResp<TUserCardRechargeNewUser> resp = AdminLteUtil.pageBeanToAdminLteDataTableResp(cloudpayCardPage,request);
		return resp.toAdminLteJsonString(TUserCardRechargeNewUser.class);
	}
}
